package org.ntu.csn.as.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ntu.csn.as.manager.DBManager;
import org.ntu.csn.as.model.Category;

/**
 * Servlet implementation class TopicSelect
 */
@WebServlet("/TopicSelectController")
public class TopicSelectController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TopicSelectController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("TopicSelectController...");
		int id = Integer.parseInt(request.getParameter("id"));
		DBManager dbm = new DBManager();
		Category c = null;
		try {
			c = dbm.getCategory(id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		request.setAttribute("category", c);
		request.setAttribute("action", "update");
		System.out.println("Selected Category : "+c.getTopic() +" Priamry Level : "+c.getGrade());
		request.getRequestDispatcher("TopicListController").forward(request,
				response);

	}

}
